package com.lvy.hczn.front.business.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lvy.hczn.front.business.common.CacheConstants;
import com.lvy.hczn.front.common.constant.Constants;
import com.lvy.hczn.front.common.constant.RedisConstants;
import com.lvy.hczn.front.common.core.redis.RedisCache;
import com.lvy.hczn.front.system.domain.business.TemperatureZone;
import com.lvy.hczn.front.system.service.GoodsBoxSpecService;
import com.lvy.hczn.front.system.service.TemperatureZoneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/mqtt/index/")
public class IndexController {

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private TemperatureZoneService temperatureZoneService;

    @Autowired
    private GoodsBoxSpecService goodsBoxSpecService;

    @RequestMapping(value = "test")
    public ModelAndView test(String clientId, String page) {
//        ModelAndView modelAndView = new ModelAndView("/test/test.html");
        ModelAndView modelAndView = new ModelAndView("wms/" + page);
        modelAndView.addObject("clientId", clientId);
        Object object = redisCache.get(CacheConstants.WEIGHT_URL);
        modelAndView.addObject("weightUrl", JSONUtil.toJsonStr(object));
        Map<String, String> pageMap = new HashMap<>();
        pageMap.put("pageType", page);
        pageMap.put("pageWarehousing", clientId);
        redisCache.setCacheMap(RedisConstants.OPERATOR_PAGE_TYPE + clientId, pageMap);
        initDataByPage(modelAndView, page);
        return modelAndView;
    }

    /**
     * 根据页面类型初始化不同值
     *
     * @param modelAndView
     * @param page
     * @return void
     * @author yw
     * @date 2023-10-15 14:58:22
     */
    public void initDataByPage(ModelAndView modelAndView, String page) {
        if (page.equals(Constants.PAGE_EMPTY)) {
            modelAndView.addObject("goodsBoxSpecList", goodsBoxSpecService.list());
            List<TemperatureZone> temperatureZoneList = temperatureZoneService.list(Wrappers.<TemperatureZone>lambdaQuery().orderByDesc(TemperatureZone::getTemperatureZoneCode));
            modelAndView.addObject("temperatureZoneList", temperatureZoneList);
        }
    }
}